<?php

/**
 * This is the model class for table "inventory".
 *
 * The followings are the available columns in table 'inventory':
 * @property integer $id
 * @property integer $item_id
 * @property integer $user_id
 * @property string $date
 * @property string $time
 * @property integer $qty_in_stock
 *
 * The followings are the available model relations:
 * @property Insert[] $inserts
 * @property InsertDetails[] $insertDetails
 * @property Item $item
 * @property User $user
 * @property PullOut[] $pullOuts
 * @property PullOutDetails[] $pullOutDetails
 */
class Inventory extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'inventory';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('item_id, user_id, qty_in_stock, date', 'required'),
			array('item_id, user_id, qty_in_stock', 'numerical', 'integerOnly'=>true),
			array('date, time', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, item_id, user_id, date, time, qty_in_stock', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'inserts' => array(self::HAS_MANY, 'Insert', 'inventory_id'),
			'insertDetails' => array(self::HAS_MANY, 'InsertDetails', 'inventory_id'),
			'item' => array(self::BELONGS_TO, 'Item', 'item_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'pullOuts' => array(self::HAS_MANY, 'PullOut', 'inventory_id'),
			'pullOutDetails' => array(self::HAS_MANY, 'PullOutDetails', 'inventory_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Inventory ID',
			'item_id' => 'Item',
			'user_id' => 'User',
			'date' => 'Date',
			'qty_in_stock' => 'Qty In Stock',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->with=array('user','item');
		$criteria->together = true;
		$criteria->compare('t.id',$this->id, true);
		$criteria->compare('user.fname',$this->user_id, true);
		$criteria->compare('item.item_desc',$this->item_id, true);
		
		$criteria->compare('id',$this->id);
	
		$criteria->compare('date',$this->date,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('qty_in_stock',$this->qty_in_stock);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Inventory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
